package com.lucenly.card.app;

import android.content.Context;

import com.lucenly.card.R;
import com.lucenly.card.base.ActivityManager;
import com.lucenly.card.constants.Constants;
import com.lucenly.card.constants.UrlDefend;
import com.lucenly.card.loader.FrescoLoader;
import com.siberiadante.toastutils.ToastApp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.umeng.commonsdk.UMConfigure;
import com.vise.log.ViseLog;
import com.vise.log.inner.LogcatTree;
import com.vise.xsnow.BaseApplication;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.interceptor.HttpLogInterceptor;
import com.vise.xsnow.loader.ILoader;
import com.vise.xsnow.loader.LoaderManager;
import com.vise.xsnow.utls.SharedPreferencesUtil;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by Adminitrator on 2018/4/18.
 */

public class MyApplication extends BaseApplication {
    private static MyApplication mInstance = null;
    public final static String TAG = "x024";
    public final static String ERROR_FILENAME = "x024_error.log";
    ILoader.Options options = new ILoader.Options(R.color.gray_de,R.color.gray_de);
    private IWXAPI api;
    @Override
    public void onCreate() {
        super.onCreate();
        getInstance();
        //设置该CrashHandler为程序的默认处理器
//        UnCeHandler catchExcep = new UnCeHandler(this);
//        Thread.setDefaultUncaughtExceptionHandler(catchExcep);
        initPrefs();
        initLog();
        initNet();
        LoaderManager.setLoader(new FrescoLoader());//外部定制图片加载库Fresco
        LoaderManager.getLoader().init(this);
        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID,true);
        api.registerApp(Constants.APP_ID);
        ToastApp.initToastUtils(this);
        JPushInterface.setDebugMode(UrlDefend.isDebug); 	// 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);     		// 初始化 JPush
        UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE,null);
    }

    public IWXAPI getApi() {
        if(api == null){
            api = WXAPIFactory.createWXAPI(this, Constants.APP_ID,true);
            api.registerApp(Constants.APP_ID);
        }
        return api;
    }

    protected void initPrefs() {
        SharedPreferencesUtil.init(getApplicationContext(), getPackageName() + "_preference", Context.MODE_PRIVATE);
    }
    private void initLog() {
        ViseLog.getLogConfig()
                .configAllowLog(!UrlDefend.isDebug)//是否输出日志
                .configShowBorders(!UrlDefend.isDebug);//是否排版显示
        ViseLog.plant(new LogcatTree());//添加打印日志信息到Logcat的树
    }
    public static synchronized MyApplication getInstance() {
        if (mInstance == null) {
            mInstance = (MyApplication) getInstance1();
        }
        return mInstance;
    }
    private void initNet() {
        ViseHttp.init(this);
        ViseHttp.CONFIG()
                //配置请求主机地址
                .baseUrl(UrlDefend.BaseUrl)
                .setCookie(true)
                //配置日志拦截器
                .interceptor(new HttpLogInterceptor()
                        .setLevel(HttpLogInterceptor.Level.BODY));

    }

    public void finishActivity() {
        ActivityManager.getInstance().finishAllActivity();
        //杀死该应用进程
        android.os.Process.killProcess(android.os.Process.myPid());
    }
}
